﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using CCWP7.Model;
using Microsoft.Phone.Reactive;

namespace CCWP7.Bare.ViewModels
{
    public class AddServerViewModel : INotifyPropertyChanged
    {
        readonly IServerDataManager ServerDataManager;
        readonly Frame Frame;

        public AddServerViewModel()
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                ServerDataManager = App.ServerDataManager;
                Frame = (Frame)App.Current.RootVisual;
            }
        }

        public const string STR_ServerUri = "ServerUri";
        string _serverUri;
        public string ServerUri
        {
            get { return _serverUri; }
            set 
            {
                _serverUri = value;
                NotifyPropertyChanged(STR_ServerUri);

                CheckShouldAddServerBeEnabled();
            }
        }

        public const string STR_Name = "Name";
        string _name;
        public string Name
        {
            get { return _name; }
            set 
            {
                _name = value;
                NotifyPropertyChanged(STR_Name);

                CheckShouldAddServerBeEnabled();
            }
        }

        void CheckShouldAddServerBeEnabled()
        {
            AddServerEnabled = ServerDataManager.IsServerUriValid(ServerUri)
                    && ServerDataManager.IsNameValid(Name);
        }

        public const string STR_AddServerEnabled = "AddServerEnabled"; 
        bool _isAddServerEnabled;
        public bool AddServerEnabled
        {
            get { return _isAddServerEnabled; }
            set 
            { 
                _isAddServerEnabled = value;
                NotifyPropertyChanged(STR_AddServerEnabled);
            }
        }

        public const string STR_IsReading = "IsReading"; 
        bool _isReading;
        public bool IsReading
        {
            get { return _isReading; }
            set 
            { 
                _isReading = value;
                NotifyPropertyChanged(STR_IsReading);
            }
        }
        
        public void AddServer()
        {
            IsReading = true;

            var fullServerUri = ServerDataManager.GetFullUri(ServerUri);

            var server = new Server(Name, fullServerUri);

            var call = Observable.FromAsyncPattern<Server, Server>(
                ServerDataManager.BeginReadAndUpdateServer,
                ServerDataManager.EndReadAndUpdateServer);

            call(server).Do(s=>ServerDataManager.AddServer(s));

            ServerDataManager.AddServer(server);

            IsReading = false;

            Frame.GoBack();
        }

        public void Cancel()
        {
            Frame.GoBack();    
        }

        public event PropertyChangedEventHandler PropertyChanged;
        void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
